package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.entity.QueryEntry;
import com.todod.mapper.SystemValueMapper;
import com.todod.model.SystemValue;
import com.todod.service.ISystemValueService;

@Service
@Transactional
public class SystemValueServiceImpl extends ServiceImpl<SystemValueMapper, SystemValue> implements ISystemValueService {

	@Autowired
	private SystemValueMapper systemValueMapper;

	public IPage<SystemValue> getListPage(QueryEntry queryEntry) {

		Page<SystemValue> page = new Page<>(queryEntry.getPage(), queryEntry.getLimit());
		IPage<SystemValue> iPage = systemValueMapper.getListPage(page, queryEntry);

		return iPage;
	}

	public void saveData(Long _userId, SystemValue systemValue) {

		systemValue.setId(SnowflakeIdGenerator.getId());

		systemValue.setStatus("1");
		systemValue.setCreateTime(LocalDateTime.now());
		systemValue.setCreateUserId(_userId);
		systemValueMapper.insert(systemValue);
	}

	public void editData(Long _userId, SystemValue systemValue) {

		systemValue.setModifyTime(LocalDateTime.now());
		systemValue.setModifyUserId(_userId);
		systemValueMapper.updateById(systemValue);

	}

	@Override
	public void updateState(Long _userId, Long code, String status) {
		SystemValue systemValue = systemValueMapper.selectById(code);

		systemValue.setStatus(status);
		systemValue.setModifyUserId(_userId);
		systemValue.setModifyTime(LocalDateTime.now());
		systemValueMapper.updateById(systemValue);
	}

	public Map<String, Object> getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		SystemValue systemValue = systemValueMapper.getInfo(code);
		map.put("systemValue", systemValue);
		return map;

	}

}
